package com.ggg.clothesshop.controller;

import com.ggg.clothesshop.model.Orders;
import com.ggg.clothesshop.model.User;
import com.ggg.clothesshop.service.OrdersService;
import com.ggg.clothesshop.utils.JWT;
import com.ggg.clothesshop.utils.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/orders")
public class OrdersController {

    @Autowired
    OrdersService ordersService;

    //获得对应订单id的订单信息
    @ResponseBody
    @RequestMapping("/selectById")
    public Orders selectById(int orderid){
        Orders order = ordersService.selectOrderById(orderid);
        return order;
    }

    //获得指定用户的订单信息
    @ResponseBody
    @RequestMapping("/selectOrdersByUser")
    public ArrayList<Orders> selectOrdersByUser(
            @RequestParam(name = "orderNum", required = false, defaultValue = "10")
                    int orderNum,int userid){
        ArrayList<Orders> list = ordersService.selectOrdersByUser(orderNum, userid);
        return list;
    }

    //获取一页订单的信息
    @ResponseBody
    @RequestMapping("/selectByPage")
    public Object selectByPage(
            @RequestParam(name = "pageNum", required = false, defaultValue = "1")
                    int pageNum,
            @RequestParam(name = "pageSize", required = false, defaultValue = "10")
                    int pageSize, int userid){

        return ordersService.selectByPage(pageNum,pageSize,userid);
    }

    //获取一页购物车订单的信息
    @ResponseBody
    @RequestMapping("/selectCartByPage")
    public Object selectCartByPage(
            @RequestParam(name = "pageNum", required = false, defaultValue = "1")
                    int pageNum,
            @RequestParam(name = "pageSize", required = false, defaultValue = "10")
                    int pageSize, int userid){

        return ordersService.selectCartByPage(pageNum,pageSize,userid);
    }

    //添加到购物车
    @ResponseBody
    @RequestMapping("/addToCart")
    public Map addToCart(Orders order){
        Map map = new HashMap();
        map.put("status",200);
        map.put("data","添加到购物车成功");
        //判断用户是否已经登陆
        if(order.getUserid()==null){
            map.put("status",401);
            map.put("data","请登录后重试");
            return map;
        }
        //添加操作的结果
        int result = ordersService.addCartOrder(order);
        //添加失败
        if(result==0){
            map.put("status",500);
            map.put("data","请求超时请重试！！");
            return map;
        }
        //查验购物车信息数量到达上限，终止添加操作
        if(result==-1){
            map.put("status",500);
            map.put("data","购物车的商品已达上限！！");
        }
        return map;
    }

    //商品下单
    @ResponseBody
    @RequestMapping("/placeOrder")
    public Map placeOrder(Orders order){

        Map map = new HashMap();
        map.put("status",200);
        map.put("data","商品下单成功成功！");

        //判断用户是否已经登陆
        if(order.getUserid()==null){
            map.put("status",401);
            map.put("data","请登录后重试");
            return map;
        }

        //下单操作
        order.setOrderstatus("待支付");
        int result = 0;
        //判断是否为购物车中的订单
        if(order.getOrderid()!=null){
            //将 ‘购物车’ 状态置为 ‘已下单’
            result = ordersService.updateOrder(order);
        }else {
            //添加新订单
            result = ordersService.addOrder(order);
        }

        //添加失败
        if(result<=0){
            map.put("status",500);
            map.put("data","请求超时请重试！！");
        }else {
            //查询刚插入订单的id并返回
            int orderId = ordersService.selectLastInsertId();
            map.put("orderId",orderId);
        }
        return map;
    }

    //删除指定订单
    @ResponseBody
    @RequestMapping("/deleteOrderById")
    public Map deleteOrderById(int orderid){
        Map map = new HashMap();
        map.put("status",200);
        map.put("data","删除商品成功");
        if(ordersService.deleteOrderById(orderid)<=0){
            map.put("status",500);
            map.put("data","删除商品失败！！");
        }
        return map;
    }


    //更新订单
    @ResponseBody
    @RequestMapping("/updateOrder")
    public Map updateOrder(Orders order){
        Map map = new HashMap();
        map.put("status",200);
        map.put("data","修改订单成功");
        if(ordersService.updateOrder(order)<=0){
            map.put("status",500);
            map.put("data","修改订单失败！！");
        }
        return map;
    }

    //支付订单
    @ResponseBody
    @RequestMapping("/pay4Order")
    public Map pay4Order(Orders order, HttpServletRequest request){

        Map map = new HashMap();
        map.put("status",200);
        map.put("data","支付成功");
        boolean flag = false;
        User user = null;

        if(order.getUserid()==null || order.getOrderid()==null){
            map.put("status",500);
            map.put("data","请提供正确的订单信息！！");
            return map;
        }

        //验证登陆状态
        Cookie[] cookies = request.getCookies();
        for(int i=0; cookies!=null && i<cookies.length; i++){
            //若能拿到对于token
            if("gggtoken".equals(cookies[i].getName())){
                String token = cookies[i].getValue();
                user = JWT.unsign(token, User.class);
                //判断是否拿到未过期的token
                if(user != null) {
                    flag = true;
                }

            }
        }

        //验证登录失败的话，直接返回
        if(flag==false){
            map.put("status",401);
            map.put("data","请先登陆！！");
            return map;
        }

        Orders oldOrder = ordersService.selectOrderById(order.getOrderid());
        //查询订单的信息是否存在
        if(oldOrder == null){
            map.put("status",500);
            map.put("data","该订单已不在，请刷新重试！！");
            return map;
        }

        //查询订单所属是否一致
        if(oldOrder.getUserid()!=user.getUserid()){
            map.put("status",500);
            map.put("data","非法操作，请刷新重试！！");
            return map;
        }

        //将订单的状态改为 ’已支付‘
        order.setOrderstatus("已支付");
        //判断修改订单
        if(ordersService.updateOrder(order)<=0){
            map.put("status",500);
            map.put("data","支付超时！！");
        }
        return map;
    }

}
